<template>
  <el-card shadow="never">
    <!-- 新增刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>

    <!-- 表格 -->

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column label="管理员">
        <template #default="{ row }">
          {{ row.admin.username }}
        </template>
      </el-table-column>
      <el-table-column label="產品名稱" :show-overflow-tooltip="true">
        <template #default="{ row }">
          {{  row.product.name }}
        </template>
      </el-table-column>
      <el-table-column prop="num" label="損耗數量" />

      <el-table-column prop="desc" label="備註" />

      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small"  type="primary" @click="handleEdit(row)"
            >修改</el-button
          >

          <el-popconfirm
            title="是否要删除?"
            confirm-button-text="確認"
            cancel-button-text="取消"
            @confirm="handleDelete(row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small"
                >删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      class="flex justify-center mt-4"
      @current-change="getData"
      :current-page="curpage"
      :page-size="limit"
    />

    <!-- 提交表单 -->
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
        <el-form-item label="產品名稱">
          <el-select v-model="form.product_id" placeholder="請選擇">
            <el-option
              v-for="item in products"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="损耗数量" prop="num">
          <el-input-number v-model="form.num" :min="1" :max="1000" />
        </el-form-item>
        <el-form-item label="備註" prop="desc">
          <el-input v-model="form.desc" type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>

<script setup>
import { ref } from "vue";

import {
  getSpoilage,
  createSpoilage,
  updateSpoilage,
  deleteSpoilage,
} from "@/api/stockpile.js";

import FormDrawer from "@/components/FormDrawer.vue";
import ListHeader from "@/components/ListHeader.vue";

import { useInitTable, useInitForm } from "@/composables/useCommon.js";

const products = ref(null);
const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  curpage,
  total,
  limit,
  getData,
  handleDelete,
} = useInitTable({
  getList: getSpoilage,
  delete: deleteSpoilage,
  onGetListSuccess: (res) => {
    products.value = res.product;

    (total.value = res.data.total), (tableData.value = res.data.data);
  },
});

//表单

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
} = useInitForm({
  form: {
    product_id: "",
    num: 1,
    desc: "",
  },
  // rules: {
  //     title: [{ required: true, message: "標題不能为空", trigger: "blur" }],
  // },
  getData,
  update: updateSpoilage,
  create: createSpoilage,
});
</script>
